package TXPathFinder;

import java.io.*;

/**
*       \class	ExternProcess
*	\brief	Gere le code C++
**/
public class ExternProcess extends Thread
{
    Process _process;
    boolean _redirectOutput;
    String _args[];

    /**
    *	\brief	Constructeur
    *   \param args[] Argument du main
    *   \param redirectOutput
    **/
    public ExternProcess(String args[], boolean redirectOutput)
    {
        _redirectOutput = redirectOutput;
        _args = args;
    }

    /**
    *	\brief	Execute le programme externe C++
    **/
    @Override public void run()
    {
        BufferedReader reader;
        String line;

        // On lance le processus externe
        try
        {
            _process = new ProcessBuilder(_args).start();
        }
        catch (IOException e1)
        {
            e1.printStackTrace();
            return;
        }
        InputStream is = _process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        reader = new BufferedReader(isr);

        // On redirige la sortie standard
        try
        {
            while ((line = reader.readLine()) != null)
            {
                if (_redirectOutput) System.out.println(line);
            }
        }
        catch (IOException e2)
        {
            e2.printStackTrace();
            return;
        }
    }
}
